console.log('tax injected');
import { transformDataByOpenWeb } from './../../lib/openWebTransform.js';
import YZ from '../commonModule/YZ.js';
import { fillHspj } from "./options/hsqj.js"
import { autoInputAccount } from "./options/autoLogin.js"
import { cwbb } from './options/cwbb.js'
import { fillInputDone, notifications } from "../common.js"

window.baseInfo = {};
if (window.$) {
  $(window).load(() => {
    window.postMessage(
      {
        event: 'getBaseInfo'
      },
      '*'
    );
  })
} else {
  setTimeout(() => {
    window.postMessage(
      {
        event: 'getBaseInfo'
      },
      '*'
    );
  }, 3000)
}


// 判断企业会计准则


window.addEventListener(
  'message',
  async function ({ data }) {
    // 只监听来自于content script的信息
    if (data.sender !== 'content-script') {
      return false;
    }
    if (data.event === 'setSaasData') {
      data = data.data;
      let sheet = data.saasData.sheet;
      if (sheet == 'sds' || sheet == 'zzs') {
        fillInput(data, sheet);
      } else if (sheet.includes('hsqj')) {
        try {
          fillHspj(data);
          fillInputDone({
            tax: 'cwbb', // 税种
            success: true, // 是否成功
            message: "汇算清缴-填报成功", // 文案
          })
          return true;
        } catch (error) {
          fillInputDone({
            tax: 'cwbb', // 税种
            success: false, // 是否成功
            message: "汇算清缴-填报失败，请联系技术人员", // 文案
          })
        }
      } else if (['balance', 'cash', 'profit'].includes(sheet)) {
        try {
          cwbb(data);
          return true;
        } catch (error) {
          fillInputDone({
            tax: 'cwbb', // 税种
            success: false, // 是否成功
            message: "财务报表-填报失败，请联系技术人员", // 文案
          })
        }
      }
    }
    if (data.event === 'getNationData') {
      window.baseInfo = data.data.baseInfo;
      getNationData(data.data.baseInfo);
    }

    if (data.event === 'getBaseInfo') {
      window.baseInfo = data.data;
      autoInputAccount(baseInfo);
    }
  },
  false
);
/**
 * 获取公司信息
 */
function getNationData(baseInfo) {
  let { institutionCode } = baseInfo.companyInfo
  let taxInfo = {
    institution: institutionCode === "1" ? '2007' : '2013', // 国税对应制度 2007 | 2013
    nsrsbh: "", // 纳税人申报号
    nsrmc: "", // 纳税人名称（纳税公司）
    skssqq: "", // 税款所属期起
    skssqz: "", // 税款所属期止
    saasInstitution: institutionCode, // 国税对应制度 2007 | 2013
  };
  window.postMessage(
    {
      event: 'backNationData',
      data: {
        taxInfo: taxInfo
      }
    },
    '*'
  );
}



function fillInput({ saasData }) {

  let sheet = saasData.sheet;

  let message = '填报成功';
  try {
    let inputData = saasData.data;

    if (sheet.includes('hsqj')) {
      hsqjFillInput(saasData);
    } else {
      if (sheet == 'sds' || sheet == 'zzs') {
        inputData = delSdsData(inputData.openParams.inputData.data.profit);
      }

      // 年报特殊处理
      if (saasData.isYearReport) {
        inputData = YZ.hackYearReportInput(
          inputData,
          saasData.lastYearData,
          saasData.sheet
        );
      }
      for (let key in inputData) {
        let value = Number.parseFloat(inputData[key]).toFixed(2) || '0.00';
        // let value = 1;
        let seletor = `input#${key}`;

        if ($(seletor).length != 0) {
          if (saasData.isYearReport) {
            $(seletor).val(value);
          } else {
            if (sheet === 'hsqj' && $(seletor).attr('disabled')) continue;
            $(seletor)
              .val(value)
              .trigger('blur');
          }
        }
      }
    }


  } catch (e) {
    message = '填报失败' + e;
  }

  // todo finally
  window.postMessage(
    {
      event: 'fillInputDone',
      data: {
        message: message
      }
    },
    '*'
  );
}

function delSdsData(tableData) {
  let sdsData = {};
  // let gsInstitution = getGsInstitution();
  sdsData.yysr = tableData.BNLJJE_1;
  sdsData.yycb = tableData.BNLJJE_2;
  sdsData.lrze = tableData.BNLJJE_30;
  return sdsData;
}
